package com.wangxinenpu.springbootdemo.controller;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wangxinenpu.springbootdemo.dataobject.vo.process.ProcessInstanceVO;
import org.activiti.api.task.model.Task;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/processes")
public class ProcessInstanceController {
    private final RuntimeService runtimeService;

    @Autowired
    public ProcessInstanceController(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    @GetMapping("/{key}")
    public List<ProcessInstanceVO> getProcessInstances(@PathVariable String key) {
        List<ProcessInstanceVO> processInstanceVOs=new ArrayList<>();
        List<ProcessInstance> processInstances= runtimeService.createProcessInstanceQuery().processDefinitionKey(key).list();
        for (ProcessInstance processInstance:processInstances){
            ProcessInstanceVO processInstanceVO=new ProcessInstanceVO();
            BeanUtils.copyProperties(processInstance,processInstanceVO);
            processInstanceVOs.add(processInstanceVO);
        }
        return processInstanceVOs;
    }


}
